﻿#ifndef DBSTRUCT_H
#define DBSTRUCT_H

#include <QVector>
#include <QString>
#include <QVariant>
#include <QSqlRecord>
#include <QLibrary>
#include <QDir>
#include <QDomDocument>
#ifndef BUILD_STATIC
# if defined(SQLOPER_LIB)
#  define SQLOPER_EXPORT Q_DECL_EXPORT
# else
#  define SQLOPER_EXPORT Q_DECL_IMPORT
# endif
#else
# define SQLOPER_EXPORT
#endif



//数据库开启完成回调函数
typedef void (*DbOpenDoneCallback)();

//锁模式
enum enumLockType
{
    enBLOCK,            //阻塞
    enNONBLOCK,         //非阻塞
};

//资源锁释放
enum enumTableInfoLock
{
    enLockNORMAL,       //自动解锁
    enLockALWAYS,       //手动解锁
    enLockSELECT,       //非SELECT执行成功或SELECT执行失败，自动解锁
};

//数据库类型
enum EDatabaseType
{
    enDBTYPE_UNKNOWN = 0,           //0:未知类型
    enDBTYPE_MYSQL,                 //1:Mysql
    enDBTYPE_ORACLE,                //2:Oracle
    enDBTYPE_SQLSERVER,             //3:sqlServer
    enDBTYPE_POSTGRESQL,            //4:PostgreSQL
    enDBTYPE_ACCESS,                //5:Access
    enDBTYPE_SQLITE,                //6:Sqlite
    enDBTYPE_OPENGAUSS,             //7:opengauss
    enDBTYPE_DAMENG,                //8:达梦
};

//数据库语句类型
enum enumSQLType
{
    enSQLCREATE,            //创建
    enSQLINSERT,            //插入
    enSQLDELETE,            //删除
    enSQLUPDATE,            //更新
    enSQLSELECT,            //查询
    enSQLINSERTBATCH,       //批量插入
    enSQLEXECUTE,           //直接执行
    enSQLCUSTOM,            //自定义（写Mysql语句，系统自动转义）
    enSQLUNKNOW,            //未知类型
};

//数据库配置信息
class CDataBaseConfig
{
public:
    QString sDBType;            //数据库类型
    QString sIP;                //数据库IP地址
    int nPort;                  //数据库端口
    QString sConnectName;       //数据库连接名
    QString sName;              //数据库名称
    QString sUserName;          //数据库用户名
    QString sPassWord;          //数据库密码
	QVector<QString> vecTableName; //数据表名
    CDataBaseConfig()
    {
        sDBType = "Mysql";
        sIP = "127.0.0.1";
        nPort = 3306;
        sConnectName = "";
        sName = "rg_sr";
        sUserName = "root";
        sPassWord = "root";
    }
};

//数据库操作使用数据
class CDatabaseExecData
{
public:
    CDatabaseExecData()
    {
        sTableName = "";
        sSql = "";
        vecField.clear();
        vecValue.clear();
        vecRecord.clear();
        sErr = "";
    }
    //添加键名和键值
    void addValue(const QString &sField, const QVariant &value)
    {
        vecField.push_back(sField);
        vecValue.push_back(value);
    }

    QString sTableName;                 //操作表名
    QString sSql;                       //执行的Sql
    QVector<QString> vecField;          //键名
    QVector<QVariant> vecValue;         //键值

    QVector<QSqlRecord> vecRecord;      //select语句执行的结果
    QString sErr;                       //错误信息
};


class SQLOPER_EXPORT ISqlOper
{
public:
	virtual ~ISqlOper() {};
	virtual bool initSql(CDataBaseConfig *pStDbCfg) = 0;
	virtual void startWork(int nTime) = 0;
	virtual void stopWork() = 0;
	virtual bool exec(CDatabaseExecData &data, const int &nLock = enNONBLOCK) = 0;

};

extern "C"
{
	SQLOPER_EXPORT ISqlOper* createISqlOper();
}




class CSqlDllOper
{
	typedef ISqlOper* (*CreateISqlOperFunc)();
public:
	CSqlDllOper(QString sTableName, QString sConfigPath, QString sDllPath);
	~CSqlDllOper();
	bool initSql();
	void startWork(int nTime = 0);
	void stopWork();
	bool exec(CDatabaseExecData &data, const int &nLock = enNONBLOCK);
private:
	ISqlOper *m_pSqlOper;   //数据库操作对象
	QLibrary* m_pLib;
	QString m_sTableName;
	QString m_sConfigPath;
	QString m_sDllPath;
	bool m_bStart;
	bool loadLibrary();
	void unLoadLibrary();
};

#endif // DBSTRUCT_H
